package com.aw.guiguzi.module.setting;

import static com.aw.baselib.util.AwAppUtils.TYPE_VERSION.TYPE_VERSION_CODE;

import android.widget.ImageView;
import android.widget.TextView;

import com.aw.baselib.base.AwBaseActivity;
import com.aw.baselib.base.AwBaseEvent;
import com.aw.baselib.util.AwAppUtils;
import com.aw.baselib.util.AwDataUtil;
import com.aw.baselib.util.AwJsonUtil;
import com.aw.baselib.util.AwLog;
import com.aw.baselib.util.AwVersionUtil;
import com.aw.baselib.util.update.AwUpdateUtil;
import com.aw.baselib.util.update.UpdateBean;
import com.aw.guiguzi.R;
import com.aw.guiguzi.base.MyApp;
import com.aw.guiguzi.bean.PGYUpdateBean;
import com.aw.guiguzi.event.EventLocalReLoginType;
import com.aw.guiguzi.event.EventRefreshMsgUnreadCountType;
import com.aw.guiguzi.util.CommonUtil;
import com.aw.guiguzi.util.ReLoginUtil;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;

public class SettingActivity extends AwBaseActivity {

    private TextView tv_version, tv_msgUnread;
    private ImageView iv_dot;

    @Override
    public void refreshByBus(AwBaseEvent type) {
        super.refreshByBus(type);
        if(type instanceof EventLocalReLoginType) {
            ReLoginUtil.INSTANCE.finishToLogin(mActivity);
        } else if(type instanceof EventRefreshMsgUnreadCountType) {
            setTextNoDefault(tv_msgUnread, String.valueOf(AwDataUtil.getIntValue((String) type.getObj())));
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_version = findViewById(R.id.tv_version);
        tv_msgUnread = findViewById(R.id.tv_msgUnread);
        iv_dot = findViewById(R.id.iv_dot);
        setToolbar("用户·设置", null);
    }

    @Override
    protected void initData() {
        super.initData();
        setText(tv_version, AwVersionUtil.getVersion(mActivity));
        if(MyApp.mUnreadMsgCount > 0) {
            setText(tv_msgUnread, String.valueOf(MyApp.mUnreadMsgCount));
            showView(tv_msgUnread, true);
        } else {
            setText(tv_msgUnread, "0");
            showView(tv_msgUnread, false);
        }

        checkAppUpdate(true);
    }

    @Override
    protected void initListener() {
        super.initListener();
        findViewById(R.id.tv_helpCenter).setOnClickListener(v -> toClass(HelpActivity.class, false));
        CommonUtil.INSTANCE.setOnClickListener(findViewById(R.id.tv_showLocalCache), () -> toClass(LocalCacheActivity.class, false));
        findViewById(R.id.tv_toModifyPwd).setOnClickListener(v -> toClass(ModifyPwdActivity.class, false));
        findViewById(R.id.tv_toSetUi).setOnClickListener(v -> toClass(SettingUiActivity.class, false));
        findViewById(R.id.tv_toSetHint).setOnClickListener(v -> toClass(SettingHintActivity.class, false));
        findViewById(R.id.tv_toSetBetting).setOnClickListener(v -> toClass(SettingBettingActivity.class, false));
        findViewById(R.id.rl_msg).setOnClickListener(v -> toClass(MsgListActivity.class, false));
        findViewById(R.id.rl_version).setOnClickListener(v -> checkAppUpdate(false));
        findViewById(R.id.tv_logout).setOnClickListener(v -> showDialogWithCancelDismiss("确认退出登录吗？", v1 -> {
            ReLoginUtil.INSTANCE.reLogin(mActivity);
        }));

    }

    private void checkAppUpdate(boolean isInitCheck) {
        PgyUpdateManager.register(mActivity, new UpdateManagerListener() {

            @Override
            public void onUpdateAvailable(String s) {
                AwLog.d("Update onUpdateAvailable: " + s);
                PGYUpdateBean pgyUpdateBean = (PGYUpdateBean) AwJsonUtil.getInstance().fromJson(s, PGYUpdateBean.class);
                if(pgyUpdateBean.getData() != null) {
                    String currentVersion = AwAppUtils.getAppVersionInfo(mActivity, AwAppUtils.TYPE_VERSION.TYPE_VERSION_NAME);
                    String compareVerson = "";
                    if(pgyUpdateBean.getData().isNeedForceUpdate()) {
                        compareVerson = pgyUpdateBean.getData().getForceUpdateVersion();
                    } else {
                        compareVerson = pgyUpdateBean.getData().getVersionName();
                    }
                    if((AwVersionUtil.compareVersions(currentVersion, compareVerson) > 0)
                            || AwDataUtil.getIntValue(AwAppUtils.getAppVersionInfo(mActivity, TYPE_VERSION_CODE)) < pgyUpdateBean.getData().getVersionCode()) {
                        UpdateBean updateBean = new UpdateBean();
                        updateBean.setAppurl(pgyUpdateBean.getData().getDownloadURL());
                        updateBean.setIsForceRenew(pgyUpdateBean.getData().isNeedForceUpdate() ? "1" : "0") ;
                        updateBean.setDescription(pgyUpdateBean.getData().getReleaseNote());
                        updateBean.setAppVersion(pgyUpdateBean.getData().getVersionName());
                        updateBean.setAppCode(pgyUpdateBean.getData().getVersionCode() + "");
                        showView(iv_dot, true);
                        if(!isInitCheck) {
                            AwUpdateUtil.getInstance(mActivity).handleUpdate(updateBean, () -> {

                            });
                        }
                    } else {
                        showView(iv_dot, false);
                        if(isInitCheck) {
                            return;
                        }
                        showMsg("已是最新版");
                    }
                } else {
                    showView(iv_dot, false);
                    if(isInitCheck) {
                        return;
                    }
                    showMsg("已是最新版");
                }
            }

            @Override
            public void onNoUpdateAvailable() {
                showView(iv_dot, false);
                AwLog.d("Update 已是最新版");
                if(isInitCheck) {
                    return;
                }
                showMsg("已是最新版");
            }

        });
    }
}
